package arffreader;

import java.io.*;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class KMeansCentroidsReader
{
    File archivo;
    FileReader fr;
    BufferedReader br;
    double clusters[][];
    String path;
    int nClusters;

    public KMeansCentroidsReader(String path) throws IOException
    {
        this.path = path;
        archivo = new File(path);
        nClusters = setNClusters();
        int nAttributes = getNAttributes();
        clusters = new double[nClusters][nAttributes];

        //String vstrings[];
        try
        {
            fr = new FileReader(archivo);
            br = new BufferedReader(fr);
            goAttributes();

            String linea;
            String attcls;
            for (int attribute = 0; attribute < nAttributes; attribute++)
            {
                linea = br.readLine();
                if(linea.contains("Clustered Instances")){
                    break;
                }
                StringTokenizer st = new StringTokenizer(linea);
                st.nextToken();
                st.nextToken();
                for(int cluster = 0; cluster < nClusters; cluster++)
                {
                    //attcls = vstrings[cluster + 2];
                    String str = st.nextToken();
                    double db = Double.parseDouble(str);
                    clusters[cluster][attribute] = db;
                }
            }
            
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(KMeansCentroidsReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        fr.close();
        br.close();
    }
    
    private void goAttributes()
    {
        String linea;
        try {
            while ((linea = br.readLine()) != null)
            {
                if(linea.contains("=================="))
                {
                    return;
                }
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(KMeansCentroidsReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private int setNClusters() throws IOException {
        int nClusters = 0;
        boolean contar = false;
        try {
            fr = new FileReader(this.path);
            br = new BufferedReader(fr);

            String linea;
            while ((linea = br.readLine()) != null) {
                if (linea.contains("Clustered Instances")) {
                    contar = true;
                }
                if (contar) {
                    if (linea.contains("%")) {
                        nClusters++;
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KMeansCentroidsReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        fr.close();
        br.close();
        return nClusters;
    }

    private int getNAttributes() throws IOException{
        int nAttributes = 0;
        String vstrings[];

        try {
            fr = new FileReader(archivo);
            br = new BufferedReader(fr);

            String linea;
            while ((linea = br.readLine()) != null) {
                if (linea.contains("Attributes:")) {
                    StringTokenizer st = new StringTokenizer(linea);
                    st.nextToken();
                    String strNAtributos = st.nextToken();
                    nAttributes = Integer.parseInt(strNAtributos);
                }
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KMeansCentroidsReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        fr.close();
        br.close();
        return nAttributes;
    }
    public int getNClusters(){
        return this.nClusters;
    }

    public double[] getEstado(int cluster){
        return clusters[cluster];
    }
}
